<template>
  <div class="pieChart">
    <div :id="pieData.pieId" style="height: 400px; width: 100%"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
export default {
  name: "pieChart",
  props: {
    // 饼图数据
    pieData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      chart: null,
      pieValue: null,
    };
  },
  mounted() {
    setTimeout(() => {
      // console.log("mountedchange");
      this.chartDes();
      this.pieCharts(this.pieData);
    }, 400);
  },
  beforeDestroy() {
    this.chartDes();
  },
  // watch: {
  //   pieData: {
  //     handler(val) {
  //       if (val) {
  //         console.log("change");
  //         this.chartDes();
  //         this.pieCharts(this.pieData);
  //       }
  //     },
  //   },
  //   deep: true,
  //   immediate: true,
  // },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    pieCharts(pieData) {
      let option = {
        // color: pieData.color,
        color: ["#67C23A", "#F56C6C"],
        // title: {
        //   text: pieData.title,
        //   textStyle: {
        //     fontSize: 20,
        //     fontWeight: 400,
        //   },
        //   // subtext: '副标题',
        //   left: "center",
        //   // top: "5%",
        // },
        tooltip: {
          trigger: "item",
        },
        legend: {
          // orient: "vertical",
          left: "center",
          bottom: "10%",
          padding: 0,
          // right: "1%",
          icon: "circle",
          itemGap: 3,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: [
          {
            name: pieData.title,
            type: "pie",
            // top: "10",
            minAngle: 5, //设置扇形的最小占比
            center: ["50%", "50%"],
            radius: "50%",
            emphasis: {
              label: {
                show: true,
                fontSize: "20",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: pieData.dataArr,
          },
        ],
      };
      this.initChart(pieData.pieId, option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
      // this.chart.on("mouseover", (params) => {
      //   this.pieValue = params.value;
      //   console.log(this.pieValue, 123);
      // });
    },
  },
};
</script>
